package de.baumann.browser.base

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import de.baumann.browser.activitys.oc.ImportWalletActivity
import de.baumann.browser.activitys.user.BindETHActivity.Companion.startBindETHActivity
import de.baumann.browser.iview.IBaseView
import de.baumann.browser.utils.getUser
import de.baumann.browser.utils.isLogined
import de.baumann.browser.views.dialog.BindEthDialog
import de.baumann.browser.views.dialog.CreateOrImportDialog
import de.baumann.browser.views.dialog.OcWalletAgreementDialog

abstract class BaseOdinFragment<V : IBaseView, P : BasePresenter<V>> : Fragment(), IBaseView {

    private var view: V? = null
    private var presenter: P? = null
    lateinit var mContext: Context

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = activity!!
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(getLayout(), container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view, savedInstanceState)
        initMvp()
        initData()
    }

    abstract fun getLayout(): Int
    abstract fun initView(view: View)
    abstract fun initData()

    fun initView(view: View, savedInstanceState: Bundle?) {
        initView(view)
    }

    open fun initMvp() {
        if (presenter == null) {
            presenter = createPresenter()
        }

        if (view == null) {
            view = createView()
        }

        if (presenter != null && view != null) {
            presenter!!.bind(view)
        }
    }

    abstract fun createPresenter(): P?
    abstract fun createView(): V?

    fun getPresenter(): P {
        return presenter!!
    }

    override fun showLoading() {
        if (activity != null) {
            (activity as BaseOdinActivity<*, *>).showLoading()
        }
    }

    override fun hideLoading() {
        if (activity != null) {
            (activity as BaseOdinActivity<*, *>).hideLoading()
        }
    }

    override fun onError(errorMsg: String?) {
        if (activity != null) {
            (activity as BaseOdinActivity<*, *>).onError(errorMsg)
        }
    }

    override fun onError(errorMsgId: Int) {
        if (activity != null) {
            (activity as BaseOdinActivity<*, *>).onError(errorMsgId)
        }
    }

    override fun tokenInvalid() {
        if (activity != null) {
//            (activity as BaseOdinActivity<*, *>).tokenInvalid()
        }
    }

    override fun notEthUrl() {
//        if (activity != null) {
//            (activity as BaseOdinActivity<*, *>).notEthUrl()
//        }
    }

    fun showLoginDialog() {
        CreateOrImportDialog(mContext)
                .setSelectListener {
                    when (it) {
                        0 -> {
                            ImportWalletActivity.startImportWalletActivity(mContext)
                            return@setSelectListener
                        }
                        1 -> {
                            OcWalletAgreementDialog(mContext).show()
                            return@setSelectListener
                        }

                    }
                }.show()

    }

    open fun onNewIntent() {

    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (presenter != null && view != null) {
            presenter?.unBind()
        }
    }

    override fun onResume() {
        super.onResume()

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
//        if (!hidden) {
//            if (isLogined()) {
//                val ethAddress = getUser()!!.eth_address
//
//                if (ethAddress == null && showBindEth()) {
//                    BindEthDialog(mContext, View.OnClickListener { view: View? -> startBindETHActivity(mContext, "") }).show()
//                }
//            }
//        }
    }


    open fun showBindEth(): Boolean {
        return true
    }

}